package com.zjitc.controller;


import com.zjitc.module.Country;
import com.zjitc.service.CountryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/20 15:43
 * @description:
 */
@Controller
public class CountryController {

  @Autowired
  private CountryService service;

  @RequestMapping("/findall")
  public ModelAndView FindAll() {
    List<Country> listcountrys = service.findAll();
    ModelAndView mv = new ModelAndView();

    mv.addObject("countries", listcountrys);
    mv.setViewName("/view/Data.jsp");

    return mv;
  }


  @RequestMapping("/searchCountry")
  public ModelAndView searchCountry(@RequestParam(name = "region") String region) {
    List<Country> lists = service.searchCountry(region);
    ModelAndView mv = new ModelAndView();

    mv.addObject("countries", lists);
    mv.setViewName("/view/Data.jsp");

    return mv;
  }

  @RequestMapping("/add")
  public View add(Country country) {
    service.add(country);
    View view = new InternalResourceView("/findall.action");
    return view;

  }

  @RequestMapping("/singlefind")
  public ModelAndView change(String code) {
    ModelAndView mv = new ModelAndView();
    Country country = service.findbycode(code);
    mv.addObject("country", country);
    mv.setViewName("/view/change.jsp");
    return mv;
  }

  @RequestMapping("/update")
  public View modify(Country country) {
    service.update(country);
    View view = new InternalResourceView("/findall.action");
    return view;
  }

  @RequestMapping("/delete")
  public View delete(String code) {
    int d = service.delete(code);
    System.out.println("成功删除" + d + "条数据");
    View mv = new InternalResourceView("/findall.action");
    return mv;
  }


}
